package com.shop.cereshop.app.controller.notice;

import com.shop.cereshop.app.page.notice.BuyerNotice;
import com.shop.cereshop.app.page.notice.NoticeDetail;
import com.shop.cereshop.app.param.notice.NoticeIdParam;
import com.shop.cereshop.app.service.notice.CereNoticeService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.common.PageParam;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 消息中心
 */
@RestController
@RequestMapping("notice")
/**
 * 注解方式生成日志对象，指定topic生成对象类名
 */
@Slf4j(topic = "NoticeController")
@CrossOrigin
@Api(value = "消息中心", tags = "消息中心")
public class NoticeController {

    @Autowired
    private CereNoticeService cereNoticeService;

    /**
     * 消息列表查询
     *
     * @return
     */
    @GetMapping("getAll")
    @ApiOperation(value = "消息列表查询")
    public Result<Page<BuyerNotice>> getAll(PageParam pageParam, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Page page = cereNoticeService.getAll(pageParam, user.getBuyerUserId());
        return new Result(page, CoReturnFormat.SUCCESS);
    }

    /**
     * 公告消息列表查询
     *
     * @return
     */
    @GetMapping("getGongGaoAll")
    @ApiOperation(value = "公告消息列表查询")
    public Result<List<BuyerNotice>> getGongGaoAll(HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        List<BuyerNotice> list = cereNoticeService.getGongGaoAll(user.getBuyerUserId());
        return new Result(list, CoReturnFormat.SUCCESS);
    }

    /**
     * 公告详情查询
     *
     * @return
     */
    @GetMapping("getById")
    @ApiOperation(value = "公告详情查询")
    public Result<NoticeDetail> getById(NoticeIdParam param, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        NoticeDetail detail = cereNoticeService.getById(param, user);
        return new Result(detail, CoReturnFormat.SUCCESS);
    }
}
